
$(function(){
	
	taskfullcalendar();
	
	//查询所有人员信息
	Otod.ajax({
		url:ctx +"/sys/emp/queryEmp.do",
		type:'post',
		successFn:function(data){
			readerMan(data);
		}
	})
	
	//查询正在进行的项目
		Otod.ajax({	
			url:ctx+"/pm/project/queryPrjUuidEmp.do",
			 type:'post',
			 successFn:function(data){
				 readerProject(data);
			 }
		 });	
		
		
		baseTaskAudit(roleNo);
		

		//添加class
		//添加点击事件
		$("#taskListUl").on("click","a",function(){
			editCodeInfo($(this).attr("key"));
		});
		//添加点击事件
		$("#prjUL").on("click","a",function(){
			editCodeInfo($(this).attr("key"));
		});
		
		//员工点击事件
		$("#selectWork").on("click","a",function(){		
			selectManWork($(this).attr("key"), $(this).attr("empName"));
			$("#selectWork a").css("color","#669fc7");
			$(this).css("color","blue");
		});
		
		
});

function baseTaskAudit(roleNo) {
	if(roleNo=="mister"||roleNo=="manager"){
		
		showRoleTaskList();
		
		selectAuditTask("SUBMIT");
		//添加点击事件
		$("#myStack li").on("click","a",function(){
			selectAuditTask($(this).attr("key"));
		});
	}else{
		showRoleTaskListOne();
		selectTask("30050002");
		//添加点击事件
		$("#myStack li").on("click","a",function(){
			selectTask($(this).attr("key"));
		});
	}
}



//将毫秒数转成日期
function transformDate(oldTime){
	if(oldTime==null){
		return "";
	}else{
		var d=new Date(oldTime);
		var year=d.getFullYear();
		var day=d.getDate();
		var month=+d.getMonth()+1;
		var hour=d.getHours();
		var minute=d.getMinutes();
		var second=d.getSeconds();
		var misec=d.getMilliseconds();
		var f=year+"-"+month+"-"+day;
		return f;
	}
}	


function editCodeInfo(uuid,prjName){
	window.location.href=ctx+"/jsp/pm/prj/step.jsp?prjId="+uuid;
};

/**
 * 根据不同的状态查询不同的数据 文档 项目 审核
 */
function selectTask(struts){
		 //得到相关的数据
		 Otod.ajax({
			 url:ctx+'/pm/task/selectStatus.do',
			 data:{struts:struts, empNo:$("#empNo").val(), roleNo:$("#roleNo").val()},
			 type:'GET',
			 successFn:function(data){
				
				renderTask(data);
			 }
		
  	});
}

/**
 * 根据不同的状态查询不同的数据
 */
function selectAuditTask(struts){
	//得到相关的数据
	Otod.ajax({
		url:ctx+'/pm/task/selectAuditTask.do',
		data:{struts:struts, empNo:$("#empNo").val(), roleNo:$("#roleNo").val()},
		type:'GET',
		successFn:function(data){
			renderRoleTaskList(data);
			//添加点击事件
			$("#taskListUl").on("click","#review",function(){
				window.location.href = ctx+"/jsp/pm/prj/auditList.jsp?empNo="+$("#empNo").val();
			});
		}
		
	});
}

/**
 * 向页面载入任务信息
 * @param data
 */
function renderTask(data){
	var showListTask = $("#showListTask").html();
	Mustache.parse(showListTask);
	var rendered = Mustache.render(showListTask,{
			data: data,
			formatDt: function(){
				return function(text,render){
					return DateUtil.dateFormat(+render(text));		//+render(text)  加号是把string转换为number类型
				};
			}
	});
	$("#taskListUl").html("<li class='text-warning bigger-110  clearfix'>"
			+"<label class='inline col-sm-4'><span class='lbl'>任务名称</span></label>"
			+"<label class='inline col-sm-4'><span class='lbl'>开始时间</span></label>"
			+"<label class='inline'><span class='lbl'>结束时间</span></label>"
			+"<label class='inline pull-right'><span class='lbl'>查看</span></label>"
			+"</li>"+rendered);
}

/**
 * 向页面载入审核信息
 * @param data
 */
function renderRoleTaskList(data){
	var showRoleListTask = $("#showRoleListTask").html();
	Mustache.parse(showRoleListTask);
	var rendered = Mustache.render(showRoleListTask,{
		data: data,
		formatDt: function(){
			return function(text,render){
				return DateUtil.dateFormat(+render(text));		//+render(text)  加号是把string转换为number类型
			};
		}
	});
	var taskVal="";
	if (data.length > 0) {
		taskVal = '<li class="text-warning bigger-110 orange clearfix item-orange">'+
		'<div class="pull-right">'+
		'<a href="#" class="blue" id="review">查看更多信息</a></div></li>'
	}
	$("#taskListUl").html(rendered+taskVal);
}

/**
 * 向页面载入审核等信息
 *
 */
function showRoleTaskList(){
	var roleTaskList = $("#show-role-task-list").html();
	Mustache.parse(roleTaskList);
	var rendered = Mustache.render(roleTaskList);
	$("#role-task-list").html("");
	$("#role-task-list").html(rendered);
}

/**
 * 向页面载入任务等信息
 *
 */
function showRoleTaskListOne(){
	var roleTaskList = $("#show-role-task-listOne").html();
	Mustache.parse(roleTaskList);
	var rendered = Mustache.render(roleTaskList);
	$("#role-task-list").html("");
	$("#role-task-list").html(rendered);
}


/**
 * 载入我的项目信息
 * @param data
 */
function readerProject(data){
	var showListProject = $("#showListProject").html();
	Mustache.parse(showListProject);
	var readered = Mustache.render(showListProject,{
		data:data,
		formatDt:function(){
			return function(text,render){
				return DateUtil.dateFormat(+render(text));//+render(text)  加号是把string转换为number类型
			};
		}
	});
	$("#prjUL").html("<li class='text-warning bigger-110  clearfix'>"
			+"<label class='inline col-sm-4'><span class='lbl'>项目名称</span></label>"
			+"<label class='inline col-sm-4'><span class='lbl'>开始时间</span></label>"
			+"<label class='inline'><span class='lbl'>结束时间</span></label>"
			+"<label class='inline pull-right'><span class='lbl'>查看</span></label>"
			+"</li>"+readered);
}

/**
 * 查询人员
 * @param data
 * @returns
 */
function readerMan(data) {
	var showListMan = $("#show-man").html();
	Mustache.parse(showListMan);
	var readered = Mustache.render(showListMan,{
		data:data
	});
	$("#selectWork").html(readered);
}

//查询人员的任务
function selectManWork(empNo,empName){
	Otod.ajax({
		url:ctx +"/pm/project/queryPrjEmp.do",
		type:"get",
		data:{empNo:empNo},
		successFn:function(data){
			$("#empNo").val(empNo);
			//查看项目
			readerProject(data);
			$("#selectEmpName").html(empName);
			
			//根据角色 显示任务 或者审核
			
			
			var roleNo = selectRoleNo(empNo);
			
			baseTaskAudit(roleNo);
			
			$("#selectEmpTask").html(empName);
			
			//日历控件
			$("#calendar").html("");
			taskfullcalendar()
			
		}
	})
}

function selectRoleNo(empNo) {
	var roleNo;
	Otod.ajax({
		url:ctx +"/sys/role/queryForEmpNo.do",
		type:"get",
		async:false,
		data:{empNo:empNo},
		successFn:function(data) {
			roleNo = data.info;
			$("#roleNo").val(roleNo);
		}
	});
	return roleNo;
}

function taskfullcalendar()	{
	/*处理日历控件 fullcalendar*/
	/*jQuery(function($) {*/
		//查询后台数据
		
		/* initialize the external events
		-----------------------------------------------------------------*/
		$('#external-events div.external-event').each(function() {
			
			// create an Event Objec	t (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/)
			// it doesn't need to have a start or end
			var eventObject = {
				title: $.trim($(this).text()) // use the element's text as the event title
			};
		
			// store the Event Object in the DOM element so we can get to it later
			$(this).data('eventObject', eventObject);
		
			// make the event draggable using jQuery UI
			$(this).draggable({
				zIndex: 999,
				revert: true,      // will cause the event to go back to its
				revertDuration: 0  //  original position after the drag
			});
			
		});
		/* initialize the calendar
		-----------------------------------------------------------------*/
		var date = new Date();
		var d = date.getDate();
		var m = date.getMonth();
		var y = date.getFullYear();
		var calendar = $('#calendar').fullCalendar({
			 buttonText: {
				prev: '<i class="icon-chevron-left"></i>',
				next: '<i class="icon-chevron-right"></i>'
			},
			header: {
				left: 'prev,next today',/* next today */
				center: 'title',
				right: 'month,agendaWeek,agendaDay'
			},
			events: function(start,end,callback) {
				 		$.ajax({
			            url:ctx+'/pm/task/queryStartEndTime.do',
						data:{
							  start:Math.round(start.getTime()/1000),
				              end: Math.round(end.getTime()/1000),
				              empNo:$("#empNo").val()
						},
						success:function(data) {
							 var events =[];
				               $(data).each(function(i,n) {
					                   events.push({
					                        title:$(this).attr('title'),
					                        start:new Date($(this).attr('startTime')), 
					                        end: new Date($(this).attr('endTime')),
					            			className: $(this).attr('className')
					                });
				                });
				               callback(events);
				            }
					});
			},		   
			/* 	 [
			{
				title: '设置一天的标题',
				start: new Date(y, m, 2),
				className: 'label-important'
			},
			{
				title: '任务标题',
				//start: new Date(y, m, d-5),
				end: new Date(y, m, d-2),
				className: 'label-success'
			},
			{
				title: 'Some Event',
				start: new Date(y, m, d-3, 16, 0),
				allDay: false
			}]  */
			editable: false,
			droppable: false, // this allows things to be dropped onto the calendar !!!
			drop: function(date, allDay) { // this function is called when something is dropped
			},
			selectable: true,
			selectHelper: true,
			select: function(start, end, allDay) {
				calendar.fullCalendar('unselect');
			},
		/* 设置日期 */
		 monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],    
		 monthNamesShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],    
		 dayNames: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],    
		 dayNamesShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"], 
		 today: ["今天"],    
		 firstDay: 1,    
		 buttonText: {    
		     today: '今天',    
		     month: '月',    
		     week: '周',    
		     day: '日',    
		     prev: '上一月',    
		     next: '下一月'    
		 },    
			eventClick: function(calEvent, jsEvent, view) {
				 //change the border color just for fun
				$(this).css('border-color', 'red');
			},	
		});
	//});
}